var allCardsArray = []

function checkForModifications() {
	var current_uniqueid = document.getElementById('uniqueid').title;
	var roomid = document.getElementById('roomid').title;
	$.ajax({
		url: "index.php",
		data: "cmd=checkModifications&current_uniqueid="+current_uniqueid,
		type: "POST",
		dataType: "json",
		success: parseModificationResults,
		error: function (xhr, ajaxOptions, thrownError){
            //alert("Exception was thrown in checkModifications");
            //alert(xhr.responseText);
        }  
	});
}

function parseModificationResults(data) {

	if (data.redirect)
		window.location = data.redirect;
	else {
		if (data.modifications == true) {
			setData(data);
			var line = document.getElementById("line");
			if (line) {
				var position = line.textLength
				line.focus();
				if (position > 0) {
					//line.setSelectionRange(position, position); 
				}
				$("#line").focus();
			}
		}
		checkForModifications();
	}
}

function parseCardData(value, card, arr) {
	
	var coins = card.corePrice.coins;
	var potions = card.corePrice.potions;
	
	var tooltip = "";
	tooltip += "<center style='color:black;'>";
	tooltip += "<u>"+card.name+"</u><br>";
	
	
	tooltip += "<img src='"+card.imageurl+"' width='150'><br>";
	//tooltip += "<img src='"+card.alternativeImageUrl+"' width='150'><br>";
		
	tooltip += "<div style='width:250px;'>"+coins+"$ ";
	if (potions > 0) {
		tooltip += potions+"&otimes;";
	}
	tooltip += "</div><br>";
	var alen = card.description.length;
	for ( var i=0, len=alen; i<len; ++i ){
		tooltip += "<div style='width:250px;'>"+card.description[i]+"</div>";
	}
	tooltip += "<br>"
	
	if (card.isRuin)
		tooltip += "ruin ";
	if (card.isCurse)
		tooltip += "curse ";
	if (card.isReaction)
		tooltip += "reaction ";
	if (card.isShelter)
		tooltip += "shelter ";
	if (card.isVictory)
		tooltip += "victory ";
	if (card.isAction)
		tooltip += "action ";
	if (card.isDuration)
		tooltip += "duration ";
	if (card.isAttack)
		tooltip += "attack ";
	if (card.isTreasure)
		tooltip += "treasure ";
	
	tooltip+= "</center>";
	card.tooltip = tooltip;
	card.imageURL = card.imageurl;
	allCardsArray[value] = card;
	
}

function saveRoomCardsData(data) {	
	$.each(data, parseCardData);
}

function getRoomCardsData() {
	$.ajax({
		url: "index.php",
		data: "cmd=getRoomCardsData",
		type: "POST",
		dataType: "json",
		success: function(data) {
			saveRoomCardsData(data);
			checkForModifications();
			checkForChatUpdates();
		},
		error: function (xhr, ajaxOptions, thrownError){
			//alert("Exception was thrown");
			//var sec_debug = document.getElementById('sec_debug');
            //sec_debug.innerHTML = xhr.responseText;
        }
	});
}

function initialLoad() {
	getRoomCardsData();
}

function checkForChatUpdates() {
	if (!document.getElementById('last_chat_id'))
		return;
	var last_chat_id = document.getElementById('last_chat_id').title;
	$.ajax({
		url: "index.php",
		data: "cmd=checkForChatUpdates&last_chat_id="+last_chat_id,
		type: "POST",
		dataType: "json",
		success: function(data) {
			updateChatLines(data);
			checkForChatUpdates();
		},
		error: function (xhr, ajaxOptions, thrownError){
            //alert("Exception was thrown in checkModifications");
            //alert(xhr.responseText);
        }  
	});
}
